package com.zbest.zpay.acctrans.share.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.zbest.zpay.acctrans.common.exception.AccTransRuntimeException;
import com.zbest.zpay.acctrans.enums.biz.AcctransBizCode;
import com.zbest.zpay.acctrans.service.model.AbstractAcctransRequest;
import com.zbest.zpay.acctrans.service.model.HoldEntity;
import com.zbest.zpay.acctrans.service.model.UnHoldEntity;
import com.zbest.zpay.acctrans.service.model.UserDepositRequest;
import com.zbest.zpay.acctrans.service.model.UserHoldDepositRequest;
import com.zbest.zpay.acctrans.service.model.UserHoldRequest;
import com.zbest.zpay.acctrans.service.model.UserUnHoldPlatformFeeRequest;
import com.zbest.zpay.acctrans.service.model.UserUnHoldRequest;
import com.zbest.zpay.acctrans.service.model.UserUnHoldTransferRequest;
import com.zbest.zpay.acctrans.service.model.UserUnHoldWithdrawRequest;
import com.zbest.zpay.acctrans.share.OperationValidateService;

/**
 * Created by wenfeihu on 16/5/25.
 */
@Service("operationValidateService")
public class OperationValidateServiceImpl implements OperationValidateService {

    @Override
    public void userHold(UserHoldRequest userHoldRequest) {
        commonValidate(userHoldRequest);
        HoldEntity holdEntity = userHoldRequest.getHoldEntity();
        if (holdEntity == null) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "holdEntity IS NULL");
        }
        holdValidate(holdEntity);
    }

    @Override
    public void userDeposit(UserDepositRequest userDepositRequest) {
        commonValidate(userDepositRequest);
        if (StringUtils.isBlank(userDepositRequest.getBusTyp())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "busTyp IS NULL");
        }
        if (StringUtils.isBlank(userDepositRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "usrId IS NULL");
        }
    }

    @Override
    public void userHoldDeposit(UserHoldDepositRequest userHoldDepositRequest) {
        commonValidate(userHoldDepositRequest);
        if (StringUtils.isBlank(userHoldDepositRequest.getBusTyp())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "busTyp IS NULL");
        }
        if (StringUtils.isBlank(userHoldDepositRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "usrId IS NULL");
        }
        HoldEntity holdEntity = userHoldDepositRequest.getHoldEntity();
        holdValidate(holdEntity);
        if (StringUtils.equals(holdEntity.getUsrId(), userHoldDepositRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "冻结的usrId和存入的usrId不一致");
        }
    }

    @Override
    public void userUnHold(UserUnHoldRequest userUnHoldRequest) {
        commonValidate(userUnHoldRequest);
        UnHoldEntity unHoldEntity = userUnHoldRequest.getUnHoldEntity();
        if (unHoldEntity == null) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "unHoldEntitys IS NULL");
        }
        unHoldValidate(unHoldEntity);
    }

    @Override
    public void userUnHoldTransfer(UserUnHoldTransferRequest userUnHoldTransferRequest) {
        commonValidate(userUnHoldTransferRequest);
        if (StringUtils.isBlank(userUnHoldTransferRequest.getFromUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "fromUsrId IS NULL");
        }
        if (StringUtils.isBlank(userUnHoldTransferRequest.getToUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "toUsrId IS NULL");
        }
        UnHoldEntity unHoldEntity = userUnHoldTransferRequest.getUnHoldEntity();
        unHoldValidate(unHoldEntity);
        if (!StringUtils.equals(unHoldEntity.getUsrId(), userUnHoldTransferRequest.getFromUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "解冻的usrId和支取的usrId不一致");
        }
    }

    @Override
    public void userUnHoldWithdraw(UserUnHoldWithdrawRequest userUnHoldWithdrawRequest) {
        commonValidate(userUnHoldWithdrawRequest);
        if (StringUtils.isBlank(userUnHoldWithdrawRequest.getBusTyp())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "busTyp IS NULL");
        }
        if (StringUtils.isBlank(userUnHoldWithdrawRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "usrId IS NULL");
        }
        UnHoldEntity unHoldEntity = userUnHoldWithdrawRequest.getUnHoldEntity();
        unHoldValidate(unHoldEntity);
        if (!StringUtils.equals(unHoldEntity.getUsrId(), userUnHoldWithdrawRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "解冻的usrId和支取的usrId不一致");
        }
    }

    @Override
    public void userUnHoldPlatformFee(UserUnHoldPlatformFeeRequest userPlatformFeeRequest) {
        commonValidate(userPlatformFeeRequest);
        if (StringUtils.isBlank(userPlatformFeeRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "UsrId IS NULL");
        }
        UnHoldEntity unHoldEntity = userPlatformFeeRequest.getUnHoldEntity();
        unHoldValidate(unHoldEntity);
        if (!StringUtils.equals(unHoldEntity.getUsrId(), userPlatformFeeRequest.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "解冻的usrId和支取的usrId不一致");
        }
    }

    private void commonValidate(AbstractAcctransRequest abstractAcctransRequest) {
        if (StringUtils.isBlank(abstractAcctransRequest.getOldTxDt())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "oldTxDt IS NULL");
        }
        if (StringUtils.isBlank(abstractAcctransRequest.getOldTxTm())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "oldTxTm IS NULL");
        }
        if (StringUtils.isBlank(abstractAcctransRequest.getOrdNo())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "ordNo IS NULL");
        }
        if (abstractAcctransRequest.getOrdAmt() == null
            || abstractAcctransRequest.getOrdAmt().getCent() <= 0) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "ordAmt IS NULL OR zero");
        }
        if (abstractAcctransRequest.getFctTxAmt() == null
            || abstractAcctransRequest.getFctTxAmt().getCent() <= 0) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "fctTxAmt IS NULL OR zero");
        }
        if (StringUtils.isBlank(abstractAcctransRequest.getVchNo())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "vchNo IS NULL");
        }
        if (StringUtils.isBlank(abstractAcctransRequest.getTxDesc())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "txDesc IS NULL");
        }
    }

    private void holdValidate(HoldEntity holdEntity) {
        if (StringUtils.isBlank(holdEntity.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "usrId IS NULL");
        }
        if (StringUtils.isBlank(holdEntity.getHldCd())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "HldCd IS NULL");
        }/*
         if (StringUtils.isBlank(holdEntity.getHldNo())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                    "HldNo IS NULL");
         }*/
        if (holdEntity.getHldAmt() == null || holdEntity.getHldAmt().getCent() <= 0) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "HldAmt IS NULL OR zero");
        }
    }

    private void unHoldValidate(UnHoldEntity unHoldEntity) {
        if (StringUtils.isBlank(unHoldEntity.getUsrId())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL, "usrId IS NULL");
        }
        if (StringUtils.isBlank(unHoldEntity.getUnHldCd())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "UnHldCd IS NULL");
        }
        if (StringUtils.isBlank(unHoldEntity.getUnHldNo())) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "UnHldNo IS NULL");
        }
        if (unHoldEntity.getUnHldAmt() == null || unHoldEntity.getUnHldAmt().getCent() <= 0) {
            throw new AccTransRuntimeException(AcctransBizCode.ACT_FIELD_NOT_NULL,
                "UnHldAmt IS NULL OR zero");
        }
    }
}
